package com.zzyl.nursing.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.collection.ListUtil;

import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.serve.dto.UserLoginRequestDto;
import com.zzyl.serve.vo.LoginVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.FamilyMemberMapper;
import com.zzyl.serve.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author lhs
 * @date 2025-05-31
 */
@Service
@Slf4j
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatServiceImpl wechatService;

    @Resource
     private TokenService tokenService;

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

    /**
     * 老人家属登录信息
     * @param dto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        //1、获取传过来的code
        LoginVo loginVo = new LoginVo();
        String code = dto.getCode();
        String phoneCode = dto.getPhoneCode();
        //2、远程调用小程序获取openid
        String openId = wechatService.getOpenId(code);
        //3、获取手机号码
        String phoneNumber = wechatService.getPhoneNumber(phoneCode);
        //4、通过openid查询数据库，如果存在，直接登录
        FamilyMember one = lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        FamilyMember familyMember = new FamilyMember();

        if (one == null) {
             //5、不存在，注册，生成随机名字
             //5.1、创建对象
              familyMember.setOpenId(openId);
               familyMember.setPhone(phoneNumber);
                familyMember.setName(getName(phoneNumber));
                save( familyMember);
            loginVo.setNickName(familyMember.getName());
        }else {
            loginVo.setNickName(one.getName());
             familyMember.setId(one.getId());
        //6、判断手机号码是否不一致，不一致更换手机号码
        if (!one.getPhone().equals(phoneNumber)) {
            one.setPhone(phoneNumber);

            updateById(familyMember);

        }

        }

//7、生成token返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId",familyMember.getId());
        claims.put("nickName", loginVo.getNickName());
        String token = tokenService.createToken(claims);

          loginVo.setToken(token);

        return loginVo;
    }

    public String getName (String phoneNumber) {
    String name =    DEFAULT_NICKNAME_PREFIX
            .get((int)(Math.random()*DEFAULT_NICKNAME_PREFIX.size()))+ StringUtils.substring( phoneNumber, 7);
    return  name;
    }

}
